package cc.flyflow.core.listeners.impl;

import cc.flyflow.common.constants.MessageTypeEnum;
import cc.flyflow.common.constants.ProcessInstanceConstant;
import cc.flyflow.common.dto.process.ProcessInstanceAssignUserRecordParamDto;
import cc.flyflow.common.dto.process.ProcessInstanceExecutionDto;
import cc.flyflow.common.dto.third.MessageDto;
import cc.flyflow.common.utils.JsonUtil;
import cc.flyflow.core.listeners.EventListenerStrategy;
import cc.flyflow.core.utils.BizHttpUtil;
import cc.flyflow.core.utils.ExecutionUtil;
import cc.flyflow.core.utils.FlowableUtils;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 任务被设置执行人了
 * <p>
 * 任务分配事件监听器
 *
 * @author Huijun Zhao;Jyl
 * @date 2023-10-10 10:12
 */
@Slf4j
@Component
public class TaskAssignedEventListener implements EventListenerStrategy, InitializingBean
{

    /**
     * 处理数据
     *
     * @param event
     * @return
     */
    @Override
    public void handle(FlowableEvent event)
    {
        //任务被指派了人员
        FlowableEntityEvent flowableEntityEvent = (FlowableEntityEvent) event;
        TaskEntityImpl task = (TaskEntityImpl) flowableEntityEvent.getEntity();

        String assignee = task.getAssignee(); //执行人id
        String tenantId = task.getTenantId();
        String executionId = task.getExecutionId();
        String parentExecutionId = ExecutionUtil.getParentExecutionId(executionId);  //查询父级执行id
        String nodeId = task.getTaskDefinitionKey(); //nodeid

        String processInstanceId = task.getProcessInstanceId();//实例id
        String processDefinitionId = task.getProcessDefinitionId();
        String flowId = FlowableUtils.getFlowId(processDefinitionId, task.getTenantId()); //流程id
        String taskType = task.getVariableLocal(ProcessInstanceConstant.VariableKey.TASK_TYPE, String.class);

        ProcessInstanceAssignUserRecordParamDto userRecordParamDto = new ProcessInstanceAssignUserRecordParamDto();
        userRecordParamDto.setFlowId(flowId);
        userRecordParamDto.setProcessInstanceId(processInstanceId);
        userRecordParamDto.setNodeId(nodeId);
        userRecordParamDto.setUserId((assignee));

        userRecordParamDto.setTaskId(task.getId());
        userRecordParamDto.setNodeName(task.getName());
        userRecordParamDto.setFlowUniqueId(FlowableUtils.getFlowUniqueId(nodeId, flowId, processInstanceId,
                task.getTenantId()));
        userRecordParamDto.setTaskType(taskType);
        userRecordParamDto.setExecutionId(executionId);

        userRecordParamDto.setTenantId(tenantId);
        userRecordParamDto.setParentExecutionId(parentExecutionId);
        BizHttpUtil.createTaskEvent(userRecordParamDto);

        //保存执行信息
        ProcessInstanceExecutionDto executionDto = new ProcessInstanceExecutionDto();
        executionDto.setExecutionId(executionId);
        executionDto.setParentIdExecutionId(parentExecutionId);
        executionDto.setProcessInstanceId(processInstanceId);
        executionDto.setNodeId(nodeId);

        executionDto.setTenantId(tenantId);
        executionDto.setFlowId(flowId);
        BizHttpUtil.saveExecution(executionDto);

        //待办消息
        //任务被创建了
        Map<String, Object> variables = task.getVariables();

        String taskId = task.getId();
        MessageDto messageDto = MessageDto.builder()
                .userId(assignee)
                .flowId(flowId)
                .processInstanceId(processInstanceId)
                .bizUniqueId(taskId)
                .tenantId(tenantId)
                .param(JsonUtil.toJSONString(variables))
                .type(MessageTypeEnum.TODO_TASK.getType())
                .readed(false).build();

        BizHttpUtil.saveMessage(messageDto);
    }

    @Override
    public void afterPropertiesSet() throws Exception
    {
        afterPropertiesSet(FlowableEngineEventType.TASK_ASSIGNED.toString());
    }
}
